package com.neuedu.web.service;

import com.neuedu.web.common.PageInfo;
import com.neuedu.web.dao.ConstantsTypeDao;
import com.neuedu.web.po.ConstantsTypePO;
import com.neuedu.web.utils.BeanConvertUtils;
import com.neuedu.web.utils.ParamUtils;
import com.neuedu.web.vo.ConstantsTypeVO;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * @author 金山
 * 项目：javaweb
 * site: https://blog.fulfill.com.cn
 * 描述  Dept 业务工呢
 * @data 2024年11月21日14:23:14
 */
public class ConstantsTypeService {

    private ConstantsTypeDao dao = new ConstantsTypeDao();


    public List list(HttpServletRequest request, HttpServletResponse response) throws IOException, NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {


        String sql = " select * from constant_type";
        List<ConstantsTypePO> list = dao.selectAll(sql);

        return BeanConvertUtils.po2Vo(list, ConstantsTypeVO.class);

    }


    public PageInfo<ConstantsTypeVO> page(HttpServletRequest request, HttpServletResponse response) throws NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {


        String sql = " select * from constant_type";


        PageInfo pageInfo = ParamUtils.getPageInfo(request);
        List<ConstantsTypePO> dataList = dao.selectPage(pageInfo.getCurrent(), pageInfo.getSize(), sql);
        int total = dao.total(sql);

        pageInfo.setTotal(total);

        // 数据
        pageInfo.setData(BeanConvertUtils.po2Vo(dataList, ConstantsTypeVO.class));

        return pageInfo;

    }

    /**
     * 保存
     * @param request
     * @param response
     * @return
     */
    public boolean saveOrUpdate(HttpServletRequest request, HttpServletResponse response) {
        ConstantsTypePO data = ParamUtils.getParam(ConstantsTypePO.class, request);
        if(data.getTypeId() != null){
            return  dao.update(data) >0;
        }else{
            return  dao.insert(data) >0;
        }
    }

    /**
     * 根据主键删除
     * @param request
     * @param response
     * @return
     */
    public boolean delete(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");

        return dao.delete(Long.parseLong(id)) >0;
    }


    /**
     * 修改前，跟住主键查询一个元素
     * @param request
     * @param response
     * @return
     * @throws NoSuchFieldException
     * @throws InvocationTargetException
     * @throws NoSuchMethodException
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    public ConstantsTypeVO getById(HttpServletRequest request, HttpServletResponse response) throws NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        String id = request.getParameter("id");
        ConstantsTypePO po = dao.selectById(Long.parseLong(id));
        return BeanConvertUtils.conver(ConstantsTypeVO.class,po) ;
    }
}
